/*
 * Copyright 2006 Kevin Wetzels 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0 
 *       
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package be.roam.drest.service.bloglines.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import be.roam.drest.service.bloglines.BloglinesEntry;
import be.roam.drest.service.bloglines.BloglinesEntryList;
import be.roam.drest.service.bloglines.BloglinesSite;
import be.roam.drest.xml.rss.Rss20Handler;
import be.roam.util.StringUtil;

/**
 * Handler for Bloglines entries.
 */
public class BloglinesEntriesHandler extends Rss20Handler<BloglinesEntryList, BloglinesSite, BloglinesEntry> {
    
    public BloglinesEntriesHandler(BloglinesEntryList entryList) {
        super(entryList);
    }
    
    /* (non-Javadoc)
     * @see be.roam.drest.xml.rss.Rss20Handler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("http://www.bloglines.com/services/module".equals(uri)) {
            if ("siteid".equals(localName) || "itemid".equals(localName) || "unreadid".equals(localName)) {
                characterBuffer = new StringBuilder();
            }
        } else {
            super.startElement(uri, localName, qName, attributes);
        }
    }

    /* (non-Javadoc)
     * @see be.roam.drest.xml.rss.Rss20Handler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("http://www.bloglines.com/services/module".equals(uri)) {
            if ("siteid".equals(localName)) {
                currentChannel.setSiteId(StringUtil.parseLong(getCharacters(true)));
            } else if ("unreadid".equals(localName)) {
                currentItem.setUnreadId(StringUtil.parseLong(getCharacters(true)));
            } else if ("itemid".equals(localName)) {
                currentItem.setItemId(StringUtil.parseLong(getCharacters(true)));
            }
        } else {
            super.endElement(uri, localName, qName);
        }
    }

    /* (non-Javadoc)
     * @see be.roam.drest.xml.rss.Rss20Handler#createChannelObject()
     */
    @Override
    protected BloglinesSite createChannelObject() {
        return new BloglinesSite();
    }

    /* (non-Javadoc)
     * @see be.roam.drest.xml.rss.Rss20Handler#createItemObject()
     */
    @Override
    protected BloglinesEntry createItemObject() {
        return new BloglinesEntry();
    }

    /* (non-Javadoc)
     * @see be.roam.drest.xml.rss.Rss20Handler#addChannelToFeed(be.roam.drest.xml.rss.Rss20Channel)
     */
    @Override
    protected void addChannelToFeed(BloglinesSite channel) {
        feed.addChannel(channel);
    }

    /* (non-Javadoc)
     * @see be.roam.drest.xml.rss.Rss20Handler#addItemToChannel(be.roam.drest.xml.rss.Rss20Item)
     */
    @Override
    protected void addItemToChannel(BloglinesEntry item) {
        currentChannel.addItem(item);
    }
    
}
